/*
 * RangedValueTest.cpp
 *
 *  Created on: 05-12-2010
 *      Author: Przemek
 */

#include "utils/RangedValue.hpp"
#include "utils/MathUtils.hpp"

#define BOOST_TEST_MODULE RangedValue test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( notTrimmingWhenInRangeTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 100));
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);

	rangedValue.changeBy(0);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);

	rangedValue.changeBy(10);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 20);

	rangedValue.changeBy(40);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 60);

	rangedValue.changeBy(-10);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 50);
}

BOOST_AUTO_TEST_CASE( trimmingWhenReachedLowerRangeWithSetValueTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 100));

	rangedValue.setValue(100);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 100);

	rangedValue.setValue(120);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 100);
}

BOOST_AUTO_TEST_CASE( trimmingWhenReachedHigherRangeWithSetValueTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 100));

	rangedValue.setValue(0);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);

	rangedValue.changeBy(-10);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);
}

BOOST_AUTO_TEST_CASE( trimmingWhenReachedLowerRangeWithChangeByTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 100));

	rangedValue.changeBy(-10);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);

	rangedValue.changeBy(-20);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 10);
}

BOOST_AUTO_TEST_CASE( trimmingWhenReachedHigherRangeWithChangeByTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 100));

	rangedValue.setValue(100);
	rangedValue.changeBy(10);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 100);

	rangedValue.changeBy(20);
	BOOST_CHECK_EQUAL(rangedValue.getValue(), 100);
}

BOOST_AUTO_TEST_CASE( calculatingPartForPositiveNumbersTest )
{
	RangedValue<int> rangedValue(Range<int> (10, 110), 60);

	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.5f), true);
	rangedValue.setValue(10);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.0f), true);
	rangedValue.setValue(110);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 1.0f), true);
	rangedValue.setValue(85);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.75f), true);
}

BOOST_AUTO_TEST_CASE( calculatingPartForNegativeNumbersTest )
{
	RangedValue<int> rangedValue(Range<int> (-110, -10), -60);

	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.5f), true);
	rangedValue.setValue(-10);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 1.0f), true);
	rangedValue.setValue(-110);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.0f), true);
	rangedValue.setValue(-85);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.25f), true);
}

BOOST_AUTO_TEST_CASE( calculatingPartForMixedNumbersTest )
{
	RangedValue<int> rangedValue(Range<int> (-50, 50), 0);

	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.5f), true);
	rangedValue.setValue(-50);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.0f), true);
	rangedValue.setValue(50);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 1.0f), true);
	rangedValue.setValue(25);
	BOOST_CHECK_EQUAL(MathUtils::isEqual(rangedValue.getFractionalPartInRange(), 0.75f), true);
}
